import Vue from 'vue'
import moment from 'moment'
import { isEmpty } from '@/utils/util'
import BigNumber from 'bignumber.js'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  if (!dataStr) return
  return moment(dataStr).format(pattern)
})

Vue.filter('state', function (dataStr) {
  if (dataStr === '' || dataStr === null) return
  if (dataStr === '1') {
    return '是'
  } else {
    return '否'
  }
})

// 金额保留两位小数
Vue.filter('retain', function (value, num = 2) {
  if (isEmpty(value)) {
    return 0
  } else {
    return new BigNumber(value).toNumber().toFixed(num)
  }
})
